import fetch from 'common/src/utils/fetch';
import utils from 'common/src/utils/utils';

/**
 * 删除校历
 */
function deleteCalendar(idList) {
    return fetch({
        url: '/api/baseresservice/schoolCalendar',
        method: 'delete',
        data: {
            ids: idList
        }
    });
}

/**
 * 修改校历
 */
function update(template) {
    return fetch({
        url: '/api/baseresservice/schoolCalendar',
        method: 'put',
        data: template
    });
}

/**
 * 新增校历
 */
function add(template) {
    return fetch({
        url: '/api/baseresservice/schoolCalendar',
        method: 'post',
        data: template
    });
}

/**
 * 查询校历
 */
function searchCalendarList(params) {
    return fetch({
        url: '/api/baseresservice/schoolCalendar/page',
        method: 'get',
        params: params
    });
}

function get(id) {
    return fetch({
        url: `/api/baseresservice/schoolCalendar/${id}`,
        method: 'get'
    });
}

function exportFile(id, name) {
    fetch({
        url: `/api/baseresservice/schoolCalendar/export/${id}`,
        method: 'get'
    }).then(res => {
        utils.downloadFile(
            res,
            name + '.doc',
            'application/msword;charset=utf-8'
        );
    });
}

function currentTermCalendar() {
    return fetch({
        url: `/api/baseresservice/schoolCalendar/currentTermCalendar`,
        method: 'get'
    });
}

function listAllCalendar() {
    return fetch({
        url: `/api/baseresservice/schoolCalendar/list`,
        method: 'get'
    });
}

function nextTermCalendar() {
    return fetch({
        url: `/api/baseresservice/schoolCalendar/nextTermCalendar`,
        method: 'get'
    });
}

/**
 * @Description: 根据选择的学期,获取上一学期
 * @author dai chang
 * @date 2018/04/22 16:20
 */
function lastTermCalendar(calendarId) {
    return fetch({
        url:
            `/api/baseresservice/schoolCalendar/lastTermCalendar?calendarId=` +
            calendarId,
        method: 'get'
    });
}

function getSchoolCalendarByTime(time){
  return fetch({
    url: `/api/baseresservice/schoolCalendar/result/time?time=`+time,
    method: 'get'
  })
}

/**
 * 通过学期，周数，星期几获取时间
 */
function getSchoolCalendarById(id,weekNum,dayOfWeek){
  return fetch({
    url: `/api/baseresservice/schoolCalendar/time?id=`+id+`&weekNum=`+weekNum+`&weekDay=`+dayOfWeek,
    method: 'get'
  })
}


export default {
    deleteCalendar: deleteCalendar,
    update: update,
    add: add,
    get: get,
    exportFile: exportFile,
    currentTermCalendar: currentTermCalendar,
    searchCalendarList: searchCalendarList,
    listAllCalendar: listAllCalendar,
    nextTermCalendar: nextTermCalendar,
    lastTermCalendar: lastTermCalendar,
    getSchoolCalendarByTime:getSchoolCalendarByTime,
    getSchoolCalendarById:getSchoolCalendarById
};
